package org.jypj.dev.util;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by RayYeung on 2016/8/16.
 */
public class DES {

    public static void main(String[] args) {
        String ENCRYPT_KEY = "dcxj@wh&";
        String s = DES.decryptDES("6WdK/eLSKip93a5j/K7NJQ==", ENCRYPT_KEY);
        String[] ns = s.split("n");
        System.out.println(s.length());

    }

    // 初始化向量，随机填充  AES 为16bytes. DES 为8bytes
    private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};

    /**
     * @param encryptString 需要加密的明文
     * @param encryptKey    秘钥(必须为8位)
     * @return 加密后的密文
     * @throws Exception
     */
    public static String encryptDES(String encryptString, String encryptKey) {
        byte[] encryptedData = new byte[0];
        try {
            // 实例化IvParameterSpec对象，使用指定的初始化向量
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            // 实例化SecretKeySpec类，根据字节数组来构造SecretKey
            SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");//DES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
            // 用秘钥初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            // 执行加密操作
            encryptedData = cipher.doFinal(encryptString.getBytes());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return Base64.encodeToString(encryptedData, Base64.DEFAULT);
    }

    /****
     * @param decrypString 密文
     * @param decryptKey   解密密钥
     * @return
     * @throws Exception
     */
    public static String decryptDES(String decrypString, String decryptKey) {

        byte[] decryptedData = new byte[0];
        try {
            byte[] byteMi = Base64.decode(decrypString, Base64.DEFAULT);
            // 实例化IvParameterSpec对象，使用指定的初始化向量
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            // 实例化SecretKeySpec类，根据字节数组来构造SecretKey
            SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            // 用秘钥初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            // 执行解密操作
            decryptedData = cipher.doFinal(byteMi);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return new String(decryptedData);
    }
}

